package com.demo.common.feign.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * feign接口传递header
 *
 * @author DayDayUp
 * create on 2023-05-22
 **/
public class FeignHeaderConfiguration implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration headerNames = request.getHeaderNames();
        if(headerNames !=null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement().toString();
                String values = request.getHeader(name);
                requestTemplate.header(name, values);
            }
        }
    }
}
